
package com.example.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;



public class ResultMessage {
	private String rspcode;
	private String rspmsg;
	// 用户想浏览器请求，浏览器响应的数据
	private Map<String, Object> extend = new HashMap<String, Object>();

	// 处理成功快捷方法
	public static ResultMessage success(String massage) {
		ResultMessage result = new ResultMessage();
		result.setRspcode("200");
		result.setRspmsg(massage);
		return result;
	}

	// 处理失败快捷方法
	public static ResultMessage error(String massage) {
		ResultMessage result = new ResultMessage();
		result.setRspcode("400");
		result.setRspmsg(massage);
		return result;
	}

	/**
	 * 链式添加法则
	 * 
	 * @author hkl
	 * @param key
	 * @param value
	 * @return
	 */
	public ResultMessage add(String key, Object value) {
		this.extend.put(key, value);
		return this;
	}

	/**
	 * 链式添加法则
	 * 
	 * @author lcb
	 * @author hkl
	 * @param value
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public ResultMessage add(Object value) {
		if (value instanceof Map) {
			Map<String, Object> m = (Map<String, Object>) value;
			for (Map.Entry<String, Object> e : m.entrySet()) {
				this.extend.put(e.getKey(), e.getValue());
			}
		} else if (value instanceof Set) {
			Set<String> set = (Set<String>) value;
			for (String str : set) {
				this.extend.put(str,str);
			}
			
		} else {
			Class<? extends Object> cls = value.getClass();
			Field[] fields = cls.getDeclaredFields();
			try {
				for (Field f : fields) {
					f.setAccessible(true);
					this.extend.put(f.getName(), f.get(value));
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}

		return this;
	}

	public String getRspcode() {
		return rspcode;
	}

	public void setRspcode(String rspcode) {
		this.rspcode = rspcode;
	}

	public String getRspmsg() {
		return rspmsg;
	}

	public void setRspmsg(String rspmsg) {
		this.rspmsg = rspmsg;
	}

	public Map<String, Object> getExtend() {
		return extend;
	}

	public void setExtend(Map<String, Object> extend) {
		this.extend = extend;
	}
}
